package com.example.user.controller;


import com.example.common.result.PageBean;
import com.example.common.result.Result;
import com.example.model.customer.pojo.Customer;
import com.example.user.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 分页查找
     * @param page
     * @param pageSize
     * @param name
     * @param phone
     * @return
     */
    @GetMapping("/admin/customers")

    public Result page(@RequestParam(value = "page",defaultValue = "1")Integer page,

                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize, String name, String phone){

        PageBean page1 = customerService.page(page, pageSize, name,phone);
        return Result.success(page1);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/admin/customers/{ids}")
    public Result delete(@PathVariable(required = true) List<Integer> ids){
        customerService.delete(ids);
        return Result.success();
    }

    /**
     * 添加数据
     * @param customer
     * @return
     */
    @PostMapping("/admin/customers")
    public Result save(@RequestBody Customer customer){
        customerService.save(customer);
        return Result.success();
    }

    /**
     * 更改回显
     * @param id
     * @return
     */
    @GetMapping("/admin/customers/{id}")
    public Result selectById(@PathVariable Integer id){
        Customer customer = customerService.selectById(id);
        return Result.success(customer);
    }

    /**
     * 更新
     * @param customer
     * @return
     */
    @PutMapping("/admin/customers")
    public Result updateById(@RequestBody Customer customer){
        customerService.updateById(customer);
        return Result.success();
    }

}
